<?php   
defined('C5_EXECUTE') or die("Access Denied.");
?>

<?php    if (!empty($field_1_textbox_text)): ?>
	<?php    $serverraw = htmlentities($field_1_textbox_text, ENT_QUOTES, APP_CHARSET); ?>
<?php    endif; ?>
<?php
	// Edit this ->
	define( 'MQ_TIMEOUT', 1 );
	// Edit this <-

    $serverdata = explode(":", $serverraw);

	require 'MinecraftServerPing.php';

	$Timer = MicroTime( true );

	$Info = false;
	$Query = null;

	try
	{
		$Query = new MinecraftPing( $serverdata[0], $serverdata[1], MQ_TIMEOUT );

		$Info = $Query->Query( );

		if( $Info === false )
		{

			$Query->Close( );
			$Query->Connect( );

			$Info = $Query->QueryOldPre17( );
		}
	}
	catch( MinecraftPingException $e )
	{
		$Exception = $e;
	}

	if( $Query !== null )
	{
		$Query->Close( );
	}

	$Timer = Number_Format( MicroTime( true ) - $Timer, 4, '.', '' );
?>
<?php if( isset( $Exception ) ): ?>
		<div class="panel panel-primary">
			<div class="panel-heading"><?php echo htmlspecialchars( $Exception->getMessage( ) ); ?></div>
			<p><?php echo nl2br( $e->getTraceAsString(), false ); ?></p>
		</div>
<?php else: ?>
		<div class="row">
				<table class="table table-bordered table-striped">
					<thead>
						<tr>
							<th colspan="2">Server Info <em>(queried in <?php echo $Timer; ?>s)</em></th>
						</tr>
					</thead>
					<tbody>
<?php if( $Info !== false ): ?>
<?php foreach( $Info as $InfoKey => $InfoValue ): ?>
						<tr>
							<td><?php echo htmlspecialchars( $InfoKey ); ?></td>
							<td><?php
	if( $InfoKey === 'favicon' )
	{
		echo '<img width="64" height="64" src="' . Str_Replace( "\n", "", $InfoValue ) . '">';
	}else if( Is_Array( $InfoValue ) )
	{
		echo "<pre>";
		print_r( $InfoValue );
		echo "</pre>";
	}
	else
	{
		echo htmlspecialchars( $InfoValue );
	}
?></td>
						</tr>
<?php endforeach; ?>
<?php else: ?>
						<tr>
							<td colspan="2">No information received</td>
						</tr>
<?php endif; ?>
					</tbody>
				</table>
		</div>
<?php endif; ?>
</div>